package com.bocloud.cmp.driver.providers.ali;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.ecs.model.v20140526.CreateVpcRequest;
import com.aliyuncs.ecs.model.v20140526.CreateVpcResponse;
import com.aliyuncs.ecs.model.v20140526.DeleteVpcRequest;
import com.aliyuncs.ecs.model.v20140526.DeleteVpcResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeVpcsRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeVpcsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.bocloud.cmp.driver.providers.VpcDriver;
import com.bocloud.common.model.BsmResult;

public class AliVpcDriver extends AliDriver implements VpcDriver {

	private static Logger logger = LoggerFactory.getLogger(AliVpcDriver.class);

	/**
	 * 
	 */
	public AliVpcDriver() {
		super();
	}

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param regionId
	 */
	public AliVpcDriver(String accessKey, String secretKey, String regionId) {
		super(accessKey, secretKey, regionId);
	}

	@Override
	public BsmResult create(String vpcName) {
		CreateVpcRequest request = new CreateVpcRequest();
		request.setVpcName(vpcName);
		CreateVpcResponse response;
		try {
			response = this.getClient().getAcsResponse(request);
			response.getRouteTableId();
			response.getVpcId();
			response.getVRouterId();
		} catch (ClientException e) {
			logger.error(":", e);
		}
		return null;
	}

	@Override
	public BsmResult remove(String vpcId) {
		DeleteVpcRequest request = new DeleteVpcRequest();
		request.setVpcId(vpcId);
		try {
			DeleteVpcResponse response = this.getClient().getAcsResponse(request);
			return new BsmResult(true, response.getRequestId(), "删除VPC成功！");
		} catch (ClientException e) {
			logger.error("remove vpc [{}] error:", vpcId, e);
			return new BsmResult(false, "");
		}
	}

	@Override
	public BsmResult list() {
		DescribeVpcsRequest request = new DescribeVpcsRequest();
		request.setPageNumber(1);
		request.setPageSize(100);
		try {
			DescribeVpcsResponse response = this.getClient().getAcsResponse(request);
			int totalCount = response.getTotalCount();
			// List<Vpc> vpcs = response.getVpcs();
			// for (Vpc vpc : vpcs) {
			// // TODO
			// }
			int totalPage = totalCount / 100 + 1;
			if (totalPage == 1) {
				return new BsmResult(true, null, "success");
			}
			for (int i = 2; i <= totalPage; i++) {
				request.setPageNumber(i);
				request.setPageSize(100);
				// response = this.getClient().getAcsResponse(request);
				// images = response.getImages();
				// for (Image image : images) {
				// models.add(this.convert(image));
				// }
			}
		} catch (ClientException e) {
			logger.error(":", e);
		}
		return null;
	}

	@Override
	public BsmResult modify() {
		// TODO Auto-generated method stub
		return null;
	}

}
